package love.yuanmengrobot.qianyugarden.controller;

import love.yuanmengrobot.qianyugarden.entity.Book;
import love.yuanmengrobot.qianyugarden.entity.BookEventAbook;
import love.yuanmengrobot.qianyugarden.entity.ThousandLanguage;
import love.yuanmengrobot.qianyugarden.entity.User;
import love.yuanmengrobot.qianyugarden.service.BookService;
import love.yuanmengrobot.qianyugarden.service.UserService;
import love.yuanmengrobot.qianyugarden.utils.Events;
import love.yuanmengrobot.qianyugarden.utils.ParamReceive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 书籍(Book)表控制层
 *
 * @author lzl
 * @since 2021-07-13 14:00:35
 */
@Controller
@RequestMapping("download")
public class BookController {
    /**
     * 服务对象
     */
    @Autowired
    BookService bookService;

    @Autowired
    UserService userService;

    /**
     *
     * @param model add request attribute
     * @return forward request
     */
    @RequestMapping("/bookList")
    public String showBook(Model model, ParamReceive params){
        List<ThousandLanguage> allLanguage = bookService.getAllLanguage();//获取语言导航栏
        model.addAttribute("languageList",allLanguage);
        //业务处理方法
        Map container = bookService.processService(params);
        //容器返回所需参数
        model.addAttribute("bookList",(List<BookEventAbook>)container.get("bookList"));
        model.addAttribute("offset",(int)container.get("offset"));
        model.addAttribute("limit",(int)container.get("limit"));
        model.addAttribute("booksCount",(int)container.get("booksCount"));
        model.addAttribute("language",(String)container.get("language"));
        model.addAttribute("order",(String)container.get("order"));
        model.addAttribute("size",(String)container.get("size"));
        model.addAttribute("time",(String)container.get("time"));

        return "download/bookList";
    }

    /**
     * @param model add request attribute
     * @return forward request
     */
    @GetMapping("/bookDetail")
    public String bookDetail(HttpServletRequest request, Model model,String bookId){
        //获取当前用户
        User currentUser = (User)request.getSession().getAttribute("UserInfo");
//        User currentUser = userService.queryById("1");
        Map contain = bookService.userEventProcess(currentUser, bookId);
        BookEventAbook bookDetail = bookService.queryByBookId(bookId);
        model.addAttribute("likeStatus",(String)contain.get("likeStatus"));
        model.addAttribute("collectionStatus",(String)contain.get("collectionStatus"));
        model.addAttribute("bookDetail",bookDetail);
        return "download/bookDetail";
    }

    /**
     * book关联的用户事件处理
     */
    @PostMapping(value = "/bookDetail/userEvent")
    @ResponseBody
    public String userEventOnBookDetail(HttpServletRequest request, @RequestBody Events events){
        //获取当前用户
        User currentUser = (User)request.getSession().getAttribute("UserInfo");
//
//        User currentUser = userService.queryById("1");
        Map contain= bookService.userEvents(currentUser, events);
        String result=(String)contain.get(events.getEventType()+"Status");

        return result;
    }
}
